During my first year of the degree, I studied a Software Development module. I had to learn the basics of Python, utilising variables, functions, loops and if/else statements.
The code snippet below is of a game I designed in Python for part of the assignment which was to create a "Game of Pairs". The ageless game where you pick a card and turn it over, you then pick another and if they match you keep them visible. Once all pairs have been found, the game is won.
As of the time of writing this, my assignment has been submitted and I am awaiting the feedback, however this is the code I wrote for this particular part of the assignment to demonstrate the new skills I've learnt.
I have really enjoyed learning the basics with Python and challenging myself to complete the assignment. I had a lot of fun and agony testing the code and getting it to work the way I wanted. On reflection, I am aware there are some constraints to my validation and error handling of which I would love to look back on this project at a later date to refine.
# Imports
import random
# FUNCTIONS
def banner():
print("-----------------------------------")
print("| Welcome to My Program |")
print("| Pairs Game |")
print("-----------------------------------\n")
def new_game():
print("Please select from 0-9 to reveal the card behind each number")
print("To win you will need to match all 5 pairs")
print("-------------------")
print("- Pairs -")
print("0 1 2 3 4 5 6 7 8 9")
print("? ? ? ? ? ? ? ? ? ?")
pairs = ["A", "A", "B", "B", "C", "C", "D", "D", "E", "E"]
board = ["?", "?", "?", "?", "?", "?", "?", "?", "?", "?"]
random.shuffle(pairs)
total_pairs = 0
turn = 1
# Game Logic
while total_pairs != 5:
print("-------------------")
print(f"Turn #{turn}")
position1 = int(input("\nWhich position would you like to flip first? "))
validated_position1 = validate_input(position1)
print(f"{pairs[validated_position1]} is behind number {validated_position1}")
position2 = int(input("\nwhich position would you like to flip second? "))
validated_position2 = validate_input(position2)
validated_number_unique = not_same_selection(validated_position1, validated_position2)
print(f"{pairs[validated_number_unique]} is behind number {validated_number_unique}")
if pairs[validated_position1] == pairs[validated_number_unique]:
print("\nYou have a match!")
update_board(board, pairs, validated_position1, validated_number_unique)
total_pairs += 1
turn += 1
else:
print("\nNo match try again\n")
turn += 1
print(f"\nCongratulations! you have completed the game in {turn} turns!\n")
play_again()
def update_board(board, pairs, x, y):
board[x] = pairs[x]
board[y] = pairs[y]
print("0 1 2 3 4 5 6 7 8 9")
for z in range(10):
print(board[z], end=" ")
print("\n")
def validate_input(response):
while response < 0 or response > 9:
response = int(input("Sorry, you need to enter a number between \"0\" and \"9\": "))
return response
def not_same_selection(x, y):
while x == y:
y = int(input(f"You have selected \"{x}\" on both positions, please try another for position 2: "))
return y
def play_again():
response = input("Do you wish to play again (\"y/n\"): ").lower()
if response == "y":
new_game()
else:
print("Thank you for playing")
def footer():
print("\n-----------------------------------")
print("| Program created by Daniel James |")
print("| 0451155@student.uwtsd.ac.uk |")
print("-----------------------------------")
# The Program
banner()
new_game()
footer()